//
//  CRRegisterView.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRRegisterView.h"
#import "CRRegisterModel.h"
#define contH  30
#define contW  200
#define contX  (self.width - 200) / 2
@interface CRRegisterView ()<UITextFieldDelegate>

@end

@implementation CRRegisterView

static CGFloat iconY = 80;
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubView];
        self.backgroundColor = [UIColor lightGrayColor];
        
        
    }
    return self;
}
- (void)setupSubView
{
    
    
    CGFloat hight = CGRectGetHeight([UIScreen mainScreen].bounds);
    if (hight == 480) {
        iconY = 50;
    }
    
    CGFloat buttonW = 80;
    CGFloat iconW = 70;
    CGFloat iconH = iconW;
    CGFloat margin = 20 + contH;
    
    self.userText = [self textFieldInitWithPlaceholder:@"用户名*"];
    self.userText.y = iconY + iconH + margin - 10;
    
    self.passwordText = [self textFieldInitWithPlaceholder:@"密码*"];
    self.passwordText.y = self.userText.y + margin;
    self.passwordText.secureTextEntry = YES;
    
    self.affirmPWText = [self textFieldInitWithPlaceholder:@"确认密码*"];
    self.affirmPWText.y = self.passwordText.y + margin;
    self.affirmPWText.secureTextEntry = YES;
    
    self.emailText = [self textFieldInitWithPlaceholder:@"手机号码*"];
    self.emailText.y = self.affirmPWText.y + margin;
    self.emailText.keyboardType = UIKeyboardTypePhonePad;
    self.iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, iconW, iconH)];
    self.iconImageView.center = self.userText.center;
    self.iconImageView.y = iconY;
    
    self.codeText = [self textFieldInitWithPlaceholder:@"请输入验证码"];
    self.codeText.y = self.emailText.y + margin;
    self.codeText.width = contW * 0.6;
    self.codeText.keyboardType = UIKeyboardTypeNumberPad;
    self.getCodeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.getCodeButton.frame = CGRectMake(contW * 0.6 + contX, self.codeText.y, contW * 0.4, contH);
    self.cancleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.cancleButton.frame = CGRectMake(contX, self.codeText.y + margin, buttonW, contH);
    
    self.registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.registerButton.frame = CGRectMake(contX + 120, self.codeText.y + margin, buttonW, contH);
    
    self.iconImageView.image = [UIImage imageNamed:@"tubiao"];
    [self.iconImageView layoutIfNeeded];
    [self.iconImageView.layer setMasksToBounds:YES];
    [self.iconImageView.layer setCornerRadius:self.iconImageView.frame.size.width / 2.0];
    
    self.codeText.placeholder = @"请输入验证码";
    [self.getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.cancleButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.registerButton setTitle:@"注册" forState:UIControlStateNormal];
    
    [self addSubview:self.iconImageView];
    [self addSubview:self.getCodeButton];
    [self addSubview:self.cancleButton];
    [self addSubview:self.registerButton];
    
}

- (void)setRegisterModel:(CRRegisterModel *)registerModel
{
    if (_registerModel != registerModel) {
        _registerModel = nil;
        _registerModel = registerModel;
    }
    [self layoutModel];
}
- (void)layoutModel
{
    _userText.text = _registerModel.userName;
    _passwordText.text = _registerModel.password;
    _affirmPWText.text = _registerModel.affirmPassword;
    _emailText.text = _registerModel.email;
    
}

- (UITextField *)textFieldInitWithPlaceholder:(NSString *)placeHolder
{
    UITextField *textField = [[UITextField alloc] init];
    
    textField.delegate = self;
    textField.placeholder = placeHolder;
    textField.x = contX;
    textField.height = contH;
    textField.width = contW;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.textAlignment = NSTextAlignmentCenter;
    [self addSubview:textField];
    
    return textField;
}

#pragma mark >>>> TextField代理

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    CGFloat offset = self.height - (textField.frame.origin.y + textField.frame.size.height + 226 + 100);
    
    if (offset <= 0) {
        [UIView animateWithDuration:0.3 animations:^{
            
            CGRect frame = self.frame;
            frame.origin.y = offset;
            self.frame = frame;
            
        }];
    }
    return YES;
}


//按return按钮的操作
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [UIView animateWithDuration:0.3 animations:^{
        
        CGRect frame = self.frame;
        
        frame.origin.y = 0.0;
        
        self.frame = frame;
        
    }];
    return YES;
}




@end
